package com.study.zzedu.ui.choicecourse;

import android.graphics.Color;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.adapter.SelectiveCourseAdapter;
import com.study.zzedu.adapter.YiDongAdapter;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.base.BaseView;
import com.study.zzedu.entity.SelectiveCourseModel;
import com.study.zzedu.entity.YiDongModel;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.widget.SpacesItemDecoration;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ChoiceCourseActivity extends BaseMvpActivity<ChoiceCourseContract.IView, ChoiceCoursePresenter> implements ChoiceCourseContract.IView{

    private RelativeLayout btnBack;
    List<SelectiveCourseModel> yiList = new ArrayList<>();
    RecyclerView rView;
    String hasSelectCount,minScore,totalScore;
    private TextView tv_title,tv_no,tv_yes;
    @Override
    protected int getLayoutID() {
        return R.layout.activity_choice_course;
    }

    @Override
    protected ChoiceCoursePresenter createPresenter() {
        return new ChoiceCoursePresenter();
    }

    @Override
    protected ChoiceCourseContract.IView createView() {
        return this;
    }

    @Override
    protected void initView() {

        btnBack = findViewById(R.id.rl_back);
        rView = (RecyclerView)findViewById(R.id.rv_list);
        tv_title = findViewById(R.id.tv_title);
        tv_no = findViewById(R.id.tv_no);
        tv_yes = findViewById(R.id.tv_yes);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rView.setLayoutManager(layoutManager);
    }

    @Override
    protected void initData() {
        getPresenter().getSelectiveCourses(ChoiceCourseActivity.this);
    }

    @Override
    protected void initListener() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Double.valueOf(totalScore) <Double.valueOf(minScore)){
                    ToastUtil.showShortToast("选课学分过低，请重新选课");
                    return;
                }
                StringBuilder sb = new StringBuilder();
                String result = "";
                for (SelectiveCourseModel d :yiList){
                    if("已选".equals(d.getStatus())){
                        sb.append(d.getKch()+",");
                    }
                }
                if(sb.length()>1){
                    result = sb.substring(0,sb.length()-1);
                }

               getPresenter().submitData(ChoiceCourseActivity.this,result);
            }
        });
    }

    @Override
    public void getSendBack(String name) {
        String ppp ="";
    }

    @Override
    public void getData(String data) {
        ToastUtil.showShortToast("可选课程查询成功");
        yiList.clear();
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jArray = jObject.getJSONArray("data");
            for (int i = 0;i<jArray.length();i++){
                JSONArray jArray1 = jArray.getJSONObject(i).getJSONArray("courseList");
                JSONObject job  = jArray.getJSONObject(i);

                hasSelectCount = String.valueOf(((int) job.getDouble("hasSelectCount")));
                minScore  = String.valueOf(job.getDouble("minScore"));
                totalScore  = String.valueOf(job.getDouble("totalScore"));
                fullString();
                for (int p =0;p<jArray1.length();p++){
                    yiList.add(new SelectiveCourseModel(
                            (float) (jArray1.getJSONObject(p).getDouble("score")),
                            jArray1.getJSONObject(p).getString("kch"),
                            jArray1.getJSONObject(p).getString("img"),
                            jArray1.getJSONObject(p).getString("introduce"),
                            jArray1.getJSONObject(p).getString("name"),
                            jArray1.getJSONObject(p).getString("status")));
                }
            }
            if (!(jArray.length() > 0)) {
                ToastUtil.showShortToast("无数据");
                return;
            }
        } catch (Exception e) {
            e.toString();
        }
        SelectiveCourseAdapter adapter = new SelectiveCourseAdapter(yiList);

        adapter.setOnCheckChange(new SelectiveCourseAdapter.OnChange() {
            @Override
            public void onChan(int posi,boolean b) {
                //ToastUtil.showShortToast("check:"+posi);
                updata(posi,b);
            }
        });
        rView.addItemDecoration(new SpacesItemDecoration(30));
        rView.setAdapter(adapter);
    }

    private void updata(int posi,boolean b){
        try{
            SelectiveCourseModel d = (SelectiveCourseModel)(yiList.get(posi));
            if(b){
                totalScore = String.valueOf(Double.valueOf(totalScore)+d.getScore());
                hasSelectCount  = String.valueOf(Integer.valueOf(hasSelectCount)+1);
            }else {
                totalScore = String.valueOf(Double.valueOf(totalScore)-d.getScore());
                hasSelectCount  = String.valueOf(Integer.valueOf(hasSelectCount)-1);
            }
            fullString();
        }catch (Exception e){
            e.toString();
        }
    }
    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    private void fullString(){
        try {
            SpannableString textSpanned1 = new SpannableString("您已选择" + hasSelectCount + "门课 共计" + totalScore + "学分\n本专业要求最低总学分为"+minScore);

            textSpanned1.setSpan(new ForegroundColorSpan(Color.RED),4, 4+hasSelectCount.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textSpanned1.setSpan(new AbsoluteSizeSpan(25, true), 4, 4+hasSelectCount.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            textSpanned1.setSpan(new ForegroundColorSpan(Color.RED),9+hasSelectCount.length(), 9+hasSelectCount.length()+totalScore.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textSpanned1.setSpan(new AbsoluteSizeSpan(25, true), 9+hasSelectCount.length(), 9+hasSelectCount.length()+totalScore.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            textSpanned1.setSpan(new ForegroundColorSpan(Color.RED),23+hasSelectCount.length()+totalScore.length(), textSpanned1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textSpanned1.setSpan(new AbsoluteSizeSpan(25, true), 23+hasSelectCount.length()+totalScore.length(), textSpanned1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv_title.setText(textSpanned1);
        }catch (Exception e){
            e.toString();
        }

    }


}